#include <filesystem>
#include <iostream>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

static std::string getWorkDirectory()
{
    std::string buffer;
    const int   bufferSize = 200;
    buffer.resize(bufferSize, 0);
    getcwd(&buffer[0], bufferSize);
    std::cout << "work dir:" << buffer << ", len:" << buffer.length() << std::endl;
    return buffer;
}

int main()
{
    char temp[] = "/home/leo/code/NoWebServer";

    std::string file          = "/home/leo/code/NoWebServer/resources/index.html";
    struct stat mmapFileState = {0};

    bool ok          = stat(file.data(), &mmapFileState) < 0;
    bool isDirectory = S_ISDIR(mmapFileState.st_mode);
    std::cout << "ok: " << ok << " dir:" << isDirectory << std::endl;
    std::cout << mmapFileState.st_size << std::endl;

    std::string assetRoot = getWorkDirectory() + "/resources/";
    std::cout << "asset Root:" << assetRoot << " len:" << assetRoot.length() << std::endl; // 200 + 11 = 211 ！！！
    for (int i = 0; i < assetRoot.length(); ++i)
    {
        std::cout << "===i: " << i << ", value:" << assetRoot[i] << std::endl;
    }

    std::string filePath = assetRoot + "/index.html";
    std::cout << "file path:" << filePath << " len:" << filePath.length() << std::endl;

    struct stat mmapFileState2 = {0};

    bool isBadFile = stat(filePath.data(), &mmapFileState2) < 0;
    bool isDir     = S_ISDIR(mmapFileState2.st_mode); // true !!!
    std::cout << "is dir: " << isDir << std::endl;

    bool beforeIsDir = std::filesystem::is_directory(file);
    file[6]          = 0;
    bool afterIsDir  = std::filesystem::is_directory(file);
    std::cout << "before is dir: " << beforeIsDir << " ,after is dir: " << afterIsDir << std::endl;
    std::cout << file << std::endl;

    const char* s1   = "hello";
    char        s2[] = "hello";
    std::cout << strlen(s1) << " " << strlen(s2) << std::endl;
    std::cout << sizeof("hello") << std::endl;
    std::cout << sizeof(s1) << std::endl;
    std::cout << sizeof(s2) << std::endl;

    return 0;
}